package ovm.gui.views.news;

import com.vaadin.ui.*;
import ovm.entity.NewsEntity;
import ovm.gui.views.AbstractView;
import ovm.management.AbstractManager;

import static ovm.application.MainApplication.navigator;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 07.06.12
 * Time: 18:50
 * To change this template use File | Settings | File Templates.
 */

public class SingleNewsView extends AbstractView {

    protected NewsEntity entity;

    public SingleNewsView(NewsEntity entity1) {
        super();

        this.entity = entity1;

        VerticalLayout singleLayout = new VerticalLayout();

        Label headerLabel = new Label();

        headerLabel.setValue(entity.getHeader());

        //headerLabel.set

        String shortMessage = entity.getMessage().substring(0, Math.min(150, entity.getMessage().length()));

        TextArea messageField = new TextArea();

        messageField.setValue(shortMessage);

        messageField.setWordwrap(true);
        //messageField.setReadOnly(true);
        messageField.setWidth(300);

        VerticalLayout footerLayout = new VerticalLayout();
        Label usernameLabel = new Label();

        usernameLabel.setValue(entity.getnUsername());
        usernameLabel.setCaption("Добавил пользователь");

        DateField dateField = new DateField("Дата сообщения", entity.getnDate());
        dateField.setReadOnly(true);

        Button editButton = new Button("Редактировать", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                navigator.setMainView(new NewsEditorView(entity));
            }
        });

        Button deleteButton = new Button("Удалить", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (AbstractManager.deleteAbstractEntity(entity) == 0) {
                    navigator.showNotification("Новость успешно удалена");
                    navigator.setMainView(new NewsView());
                } else {
                    navigator.showNotification("Ну че за пиздец???");
                }
            }
        });

        editButton.setStyleName("link");
        deleteButton.setStyleName("link");

        footerLayout.addComponent(dateField);
        footerLayout.addComponent(usernameLabel);
        footerLayout.addComponent(editButton);
        footerLayout.addComponent(deleteButton);


        singleLayout.addComponent(headerLabel);
        singleLayout.addComponent(messageField);
        singleLayout.addComponent(footerLayout);

        addComponent(singleLayout);

    }

}
